using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class CTTKKeToanLuongTinhDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveCTTKKeToanLuongTinh(CTTKKeToanLuongTinh cttkketoanluongtinh)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddCTTKKeToanLuongTinh", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddCTTKKeToanLuongTinh";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, cttkketoanluongtinh.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@TableName", DbType.String, ParameterDirection.Input, cttkketoanluongtinh.TableName);
            cmd.Parameters.Add(param);
			param = CreateParameter("@DoiTuong", DbType.String, ParameterDirection.Input, cttkketoanluongtinh.DoiTuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TaiKhoan", DbType.String, ParameterDirection.Input, cttkketoanluongtinh.TaiKhoan);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoDuBanDau", DbType.Int64, ParameterDirection.Input, cttkketoanluongtinh.SoDuBanDau);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all CTTKKeToanLuongTinh in DB
		public ResultObj GetAllCTTKKeToanLuongTinh( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTTKKeToanLuongTinh> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTTKKeToanLuongTinh", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTTKKeToanLuongTinh>();
			CTTKKeToanLuongTinh item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTTKKeToanLuongTinh();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.TableName = Convert.ToString(reader["TableName"]);
			item.DoiTuong = Convert.ToString(reader["DoiTuong"]);
			item.TaiKhoan = Convert.ToString(reader["TaiKhoan"]);
			item.SoDuBanDau = Convert.ToInt64(reader["SoDuBanDau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all CTTKKeToanLuongTinh in DB with condition
		public ResultObj GetAllCTTKKeToanLuongTinhByID(CTTKKeToanLuongTinh cttkketoanluongtinh)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTTKKeToanLuongTinh> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTTKKeToanLuongTinhByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, cttkketoanluongtinh.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTTKKeToanLuongTinh>();
			CTTKKeToanLuongTinh item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTTKKeToanLuongTinh();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.TableName = Convert.ToString(reader["TableName"]);
			item.DoiTuong = Convert.ToString(reader["DoiTuong"]);
			item.TaiKhoan = Convert.ToString(reader["TaiKhoan"]);
			item.SoDuBanDau = Convert.ToInt64(reader["SoDuBanDau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        public ResultObj GetAllCTTKKeToanLuongTinhForBCDTK(string MaSoTK)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<CTTKKeToanLuongTinhForBCDTK> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllCTTKKeToanLuongTinhForBCDTK", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@MaSoTK", DbType.String, ParameterDirection.Input, MaSoTK);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<CTTKKeToanLuongTinhForBCDTK>();
                CTTKKeToanLuongTinhForBCDTK item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new CTTKKeToanLuongTinhForBCDTK();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.TableName = Convert.ToString(reader["TableName"]);
                    item.DoiTuong = Convert.ToString(reader["DoiTuong"]);
                    item.TaiKhoan = Convert.ToString(reader["TaiKhoan"]);
                    item.SoDuBanDau = Convert.ToInt64(reader["SoDuBanDau"]);

                    item.TenChiNhanh = Convert.ToString(reader["TenChiNhanh"]);
                    item.TenKhachHang = Convert.ToString(reader["TenKhachHang"]);
                    item.TenNganHang = Convert.ToString(reader["TenNganHang"]);
                    item.TenNhaCungCap = Convert.ToString(reader["TenNhaCungCap"]);
                    item.HoTen = Convert.ToString(reader["HoTen"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
		
		// Delete a CTTKKeToanLuongTinh out of DB with condition
		public ResultObj DeleteCTTKKeToanLuongTinh(CTTKKeToanLuongTinh cttkketoanluongtinh)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteCTTKKeToanLuongTinh", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteCTTKKeToanLuongTinh";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, cttkketoanluongtinh.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
					
	}
}
